﻿Imports System.IO
Imports System.Net.Mail
Imports System.Network.Communication.Email

Public Class Listener
    Public Property Index() As Integer = 0

    Public Sub New()
        Me.Index = 0
    End Sub

    Public Sub New(ByVal indx As Integer)
        Me.Index = indx
    End Sub

    Public Shared Narrowing Operator CType(ByVal inx As Integer) As Listener
        Return New Listener(inx)
    End Operator

    Public Sub WriteLine(ByVal fmt As String, ByVal ParamArray arg() As Object)
        LibDebug.DebugPrint(Me.Index, Now.ToString() & ": " & String.Format(fmt, arg))
    End Sub

End Class

Public Class LibDebug
    Public Shared Property AuthorMail() As New Email

    Public Property DataListener() As Listener = 0
    Public Property TraceListener() As Listener = 0

    Public Sub New()
        If IO.File.Exists("Data.log") Or IO.File.Exists("Trace.log") Then
            SendMailToAuthor(AuthorMail)
        End If
        If IO.File.Exists("Data.log") Then Kill("Data.log")
        If IO.File.Exists("Trace.log") Then Kill("Trace.log")
        Me.DataListener = Debug.Listeners.Add(New System.Diagnostics.TextWriterTraceListener("Data.log", "Data"))
        Me.TraceListener = Debug.Listeners.Add(New System.Diagnostics.TextWriterTraceListener("Trace.log", "Trace"))
    End Sub

    Public Function SendMailToAuthor(ByVal m As Email) As Boolean
        If IO.File.Exists("Trace.log") Then
            With m
                .Subject = Now.ToString() & My.Application.Info.ProductName
                .Body = Me.ToString()
                Return .Send()
            End With
        Else
            Return True
        End If
    End Function

    Public Overrides Function ToString() As String
        Dim sb As New System.Text.StringBuilder
        If IO.File.Exists("Trace.log") Then
            Dim txt = My.Computer.FileSystem.ReadAllText("Trace.log")
            sb.AppendLine("Trace.log")
            sb.AppendLine(txt)
            If IO.File.Exists("Data.log") Then
                txt = My.Computer.FileSystem.ReadAllText("Data.log")
                sb.AppendLine("Data.log")
                sb.AppendLine(txt)
            End If
        End If
        Return sb.ToString()
    End Function

    Public Function ToLogBytes() As Byte()
        Dim lst As New Generic.List(Of System.Tuple(Of String, String))
        If IO.File.Exists("Data.log") Then
            Dim txt = My.Computer.FileSystem.ReadAllText("Data.log")
            lst.Add(New System.Tuple(Of String, String)("Data.log", txt))
        End If
        If IO.File.Exists("Trace.log") Then
            Dim txt = My.Computer.FileSystem.ReadAllText("Trace.log")
            lst.Add(New System.Tuple(Of String, String)("Trace.log", txt))
        End If
        Using ms As New IO.MemoryStream()
            Using gzipStream As New Compression.GZipStream(ms, Compression.CompressionMode.Compress, True)
                Dim bytesData = Serialize(lst)
                gzipStream.Write(bytesData, 0, bytesData.Length)
                gzipStream.Close()
            End Using
            ToLogBytes = ms.ToArray()
            ms.Close()
        End Using
    End Function

    Public Shared Sub DebugPrint(ByVal Name As String, ByVal Obj As Object)
        For i As Integer = 0 To Debug.Listeners.Count
            If Debug.Listeners.Item(i).Name = Name Then
                SyncLock Debug.Listeners.Item(i)
                    Debug.Listeners.Item(i).WriteLine(Obj)
                End SyncLock
                Exit Sub
            End If
        Next
    End Sub

    Public Shared Sub DebugPrint(ByVal i As Integer, ByVal Obj As Object)
        If i >= 0 And i < Debug.Listeners.Count Then
            SyncLock Debug.Listeners.Item(i)
                Debug.Listeners.Item(i).WriteLine(Obj)
                Debug.Listeners.Item(i).Flush()
            End SyncLock
        End If
    End Sub

End Class